package com.tz.online.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**工具类:注册驱动,获取数据库连接,释放资源**/
public class JdbcUtils {
	
	private static String url = DBConfigFactory.getValue("url");
	private static String user = DBConfigFactory.getValue("user");
	private static String password = DBConfigFactory.getValue("password");
	
	/**
	 * 注册驱动,获取数据库连接
	 * @throws SQLException 
	 */
	public static Connection getConnection() throws SQLException{
		Connection conn = null;
		try {
			Class.forName(DBConfigFactory.getValue("driver"));
		    
			conn = DriverManager.getConnection(url, user, password);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 释放资源
	 */
	public static void closeResources(Connection conn,Statement st,ResultSet rs){
		if(null!=rs){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(null!=st){
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(null!=conn){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
